﻿using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace TestAutoFac
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            var builder = new ContainerBuilder();
            builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();
            // 把当前程序集中的 Controller  都注册
            // 不要忘了.PropertiesAutowired() ，作用：使用别的 Service 依赖
            //  获取所有相关类库的程序集
            Assembly[] assemblies = new Assembly[] { Assembly.Load("ZSZ.DAL") ,
                Assembly.Load("ZSZ.DALFactory") ,
                Assembly.Load("ZSZ.Service") };
            builder.RegisterAssemblyTypes(assemblies).Where(type => !type.IsAbstract)
            .AsImplementedInterfaces().PropertiesAutowired();

            //注册程序集下所有类
            builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).PropertiesAutowired();

            var container = builder.Build();
            // 注册系统级别的 DependencyResolver ，这样当 MVC  框架创建 Controller  等对象的时候都是管 Autofac  要对象。
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}
